<?php
// +----------------------------------------------------------------------
// | makePHP 
// +----------------------------------------------------------------------
// | Copyright (c) 2009 http://makephp.org All rights reserved.
// +----------------------------------------------------------------------
// | Licensed BSD.(http://www.make.org/BSD-license.txt)
// +----------------------------------------------------------------------
// | Author: 叁石(sanshi) <sanshi0815@tom.com>
// | Author: 白家昌<baijc2004cn@126.com>
// +----------------------------------------------------------------------
/**
 *  Entrance File of the Control class.
 *
 * @Author:    Mirze<mirze@foxmail.com>
 * @Package:   Interface of the View class. 
 * @Version:   $Id: zy_arrival_log.class.php 2011-07-07 11:19:53 $
 * @Show Note: 到货记录
 */
Class zy_arrival_log extends TheAdmin
{
	/* DataBase object */
	public $db;

	/* View the data page limit items */
	public $pageSize = PAGENO;
	
	/**
	 *	Construct this module class.
	*/
	public function zy_arrival_log()
	{
	}
	public function set()
	{
		$this->TheAdmin();
		//定义模版
		$this->tplFile ="zy_arrival_log.tpl";
		$this->admin_user_id = $this->userRequest->get('admin_user_id');
		//取得数据库连接
		$this->db = Factory::getDB("zy_arrival_log");
		//取得用户对于该类的权限
		$user_p = $this->get_user_popedom_by_class_name(__CLASS__);
		$class_p_arr = $this->show_p();
		//取得该类的权值数组
		$class_method = $class_p_arr['class_method'];
		//取得用户操作菜单
		$menu = array();
		foreach ($class_method as $k=>$v)
		{
			if( (parse_power($user_p,$v[2]) && (isset($v[3]) ?$v[3] :false)) || IS_ROOT)
			{
				array_push($menu,array('url'=>"?c=".__CLASS__."&m={$v[0]}",'title'=>$v[1]));
			}
		}
		//用户有权菜单生成 可以添加其他操作
		$this->smarty->assign('menus',$menu);
		$this->smarty->assign('area_arr',get_area_arr());//到货地址
		$this->smarty->assign("main_menus",$this->get_user_menus());
	}
	
	/**
	 *	Call class construct method.
	*/
	public function __construct()
	{
			$this->zy_arrival_log();
	}
	public function _display()
	{
		$this->smarty->display($this->tplFile);
	}
	/**
	 *	default method.
	*/
	public function show()
	{
		$col = "arrival_log_id";
		$where = 1;
		$rowNo = $this->db->getSumNumber($this->db->tableName,$where);
		//数据库执行sql语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if( 0 > $rowNo) goToIndex($rowNo);
		//分页
		$page = Factory::getBaseClass('pagination');
		$page->set($rowNo,$this->pageSize);
		$numrows = $this->pageSize;
		$offset  = $page->getSqlMove();
		$arr = $this->db->getAll($where,$col,$numrows,$offset);
		admin_log($this->admin_user_id,__METHOD__,'查看',1);
		//数据库执行select语句出错
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		if(0 > $arr ) goToIndex($arr);
		
		$this->smarty->assign('arr',$arr);
		//默认方法
		$this->smarty->assign("page_footer",$page->getFooter().$page->getJumpPage());
		$this->smarty->assign('act','list');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	add method.
	*/
	public function add()
	{
		$sell_reply_id = isset($_REQUEST['sell_reply_id']) ? intval($_REQUEST['sell_reply_id']) : 0;//执行批复关联表ID
		$contract_no = isset($_REQUEST['contract_no']) ? trim($_REQUEST['contract_no']) : 0;//合同号
		//添加数据
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			$mc_log_id = isset($_REQUEST['mc_log_id']) ? intval($_REQUEST['mc_log_id']) : 0;//执行记录ID
			if (empty($mc_log_id)) {
				alertMsg('-2077');
			}
			//根据设备记录ID取得剩余到货数
			$zy_arrival_log_db = Factory::getDB('zy_arrival_log');
			$zy_mc_log_db = Factory::getDB('zy_mc_log');
			$mc_log_num = $zy_arrival_log_db->get_arrival_no($mc_log_id);
			$zy_mc_log_arr = $zy_mc_log_db->getOne($mc_log_id);
			/*print_r($mc_log_num);
			print_r($zy_mc_log_arr);*/
			$last_num = ($zy_mc_log_arr['mc_no'] - $mc_log_num) > 0 ?  ($zy_mc_log_arr['mc_no'] - $mc_log_num) : 0;
			//使用模版，展示提交界面
			$this->smarty->assign('mc_log_id',$mc_log_id);
			$this->smarty->assign('contract_no',$contract_no);
			$this->smarty->assign('sell_reply_id',$sell_reply_id);
			$this->smarty->assign('last_num',$last_num);
			$this->smarty->assign('act_title','添加');
			$this->smarty->assign('act','add');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			$temp = $this->getValue();
			$insrt = $this->db->insert_zy_arrival_log($temp);
			$error_no = 0 > $insrt ?  $insrt :'-1501';
			admin_log($this->admin_user_id,__METHOD__,"id={$insrt}",2);
			if ($insrt > 0) {
				//更新数量记录表中数据
		        $this->update_no_log_date($temp['mc_log_id'],$temp['arrival_no']);
			}
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			if ($insrt < 0) {
				alertMsg($error_no, "index.php?c=zy_sell&m=browse_equipment&contract_no={$contract_no}&sell_reply_id={$sell_reply_id}");
			}else{
				redirectPage("index.php?c=zy_sell&m=browse_equipment&contract_no={$contract_no}&sell_reply_id={$sell_reply_id}");
			}
		}
	}
	
	/**
	 *	edit method.
	*/
	public function edit()
	{
		//取得id，也就是唯一索引
		$arrival_log_id = isset($_REQUEST['arrival_log_id']) ? intval(trim($_REQUEST['arrival_log_id'])) : 0 ;
		//取得是否查看详情
		$is_show = isset($_REQUEST['is_show']) ? intval(trim($_REQUEST['is_show'])) : 0 ;
		if(!isset($_REQUEST['post']) || trim($_REQUEST['post'])!=1)
		{
			//使用模版，展示提交界面
			$result = $this->db->getOne($arrival_log_id);
			if(empty($result))
			{
				alertMsg('-2001', 'index.php?c=zy_arrival_log');
			}
			foreach ($result AS $k=>$v)
			{
				$this->smarty->assign("{$k}",$v);
			}
			$this->smarty->assign('is_show',$is_show);
			$this->smarty->assign('act_title','修改');
			$this->smarty->assign('act','edit');
			$this->smarty->display($this->tplFile);
		} else {
			//用户提交，把数据插入数据库
			//设置where条件
			$where = "arrival_log_id=$arrival_log_id";
			$temp = $this->getValue(1);
			$update = $this->db->update_zy_arrival_log($temp,$where);
			admin_log($this->admin_user_id,__METHOD__,$where,2);
			$error_no = 0 > $update ?  $update :'-1502';
			//取得错误的sql语句
			//$error_sql = end($this->db->getDebugMsg());
			//echo $error_sql;
			alertMsg($error_no, 'index.php?c=zy_arrival_log');
		}
	}
	
	/**
	 *	browse method.
	*/
	public function browse() {
		$arrival_log_id = isset($_REQUEST['arrival_log_id']) ? intval(trim($_REQUEST['arrival_log_id'])) : 0 ;
		//使用模版，展示提交界面
		$result = $this->db->getOne($arrival_log_id);
		if(empty($result))
		{
			alertMsg('-2001', 'index.php?c=zy_arrival_log');
		}
		foreach ($result AS $k=>$v)
		{
			$this->smarty->assign("{$k}",$v);
		}
		//$this->smarty->assign('is_show',$is_show);
		$this->smarty->assign('act_title','查看');
		$this->smarty->assign('act','browse');
		$this->smarty->display($this->tplFile);
	}
	
	/**
	 *	del method.
	*/
	public function del()
	{
		//取得id，也就是唯一索引
		$arrival_log_id = isset($_REQUEST['arrival_log_id']) ? intval(trim($_REQUEST['arrival_log_id'])) : 0 ;
		$where = "arrival_log_id=$arrival_log_id";
		$del=$this->db->delete_zy_arrival_log($where);
		admin_log($this->admin_user_id,__METHOD__,$where,3);
		$error_no = 0 > $del ?  $del :'-1503';
		//取得错误的sql语句
		//$error_sql = end($this->db->getDebugMsg());
		//echo $error_sql;
		alertMsg($error_no, 'index.php?c=zy_arrival_log');
	}
	
	/**
	 *	get request method.
	*/
	public function &getValue($isEdit=0)
	{
		$temp = array();
		$check = Factory::getBaseClass("checkBase");
		if($isEdit){
			$temp['arrival_log_id'] = $_POST['arrival_log_id'];
		} else {
			$temp['arrival_log_id'] = "";
		}
		$temp['mc_log_id'] = isset($_POST['mc_log_id']) ? intval($_POST['mc_log_id']) : 0;//执行记录ID
		if (empty($temp['mc_log_id'])) {
			alertMsg('-2077');
		}
		//补充其他的字段
		$temp['arrival_area'] = isset($_POST['arrival_area']) ? htmlspecialchars(trim($_POST['arrival_area'])) : '';//到货地点				
		$temp['arrival_time'] = isset($_POST['arrival_time']) && !empty($_POST['arrival_time']) ? strtotime($_POST['arrival_time']) : 0;
		$temp['arrival_no'] = isset($_POST['arrival_no']) ? intval($_POST['arrival_no']) : 1;//到货数
		//到货数不能大于够买数
		$zy_no_log_arr = $this->get_no_log($temp['mc_log_id']);		//取得数量记录表中数据
		if ($zy_no_log_arr['sell_no'] < $temp['arrival_no'] + $zy_no_log_arr['arrival_no']) {
			alertMsg('-2078');
		}
				
		admin_log($this->admin_user_id,__METHOD__,serialize($temp),2);	
		return $temp;
	}
	/**
	 *	get class method and popedom.
	*/
	public function show_p()
	{
		//该方法为，提供用户使用方法定义，非web用户权限相关不用写
		return array(
			/* 定义该类中文名称*/
			'class_name'=>array(__CLASS__,'到货记录'),
			'class_method' =>array(
				//array（方法名，中文解释，权值,是否为菜单）
				'show'  => array('show','列表',1,true),
				'add'  => array('add','添加',2,true),
				'edit'  => array('edit','编辑',4,false),
				'del'  => array('del','删除',8,false),
				'browse' => array('browse', '查看', 16, false),
				'show_p'=> array('show_p','',0,false)
			)
		);
	}
	/**
	 * 根据操作记录ID取得数量记录表中数据
	 *
	 * @param unknown_type $mc_log_id
	 * @return unknown
	 */
	function get_no_log($mc_log_id)
	{
		//到货数不能大于够买数
		$zy_no_log_db = Factory::getDB('zy_no_log');
		//取得数量记录表中数据
		return $zy_no_log_arr = $zy_no_log_db->getOne($mc_log_id);
	}
	/**
	 * 更新数据表中数据
	 *
	 * @param unknown_type $mc_log_id  执行记录ID
	 * @return unknown
	 */
	function update_no_log_date($mc_log_id,$num)
	{
		$where = "mc_log_id = {$mc_log_id} ";
		$zy_no_log_db = Factory::getDB('zy_no_log');
 		$temp = $zy_no_log_db->getAll($where,'',1,0);
 		$arrival_no = $num + $temp[0]['arrival_no'];
 		$values = array(
 					'arrival_no'=>$arrival_no,
 					);
 		
 		$re = $zy_no_log_db->update_zy_no_log($values,$where);
 		//判断如果已经有到货  并且没有全部到货 则调整为设备为到货中
 		
 		//判断如果到货数等于购买数 则更新执行记录表中设备到货状态
 		if ($re > 0 ) {
 			$zy_mc_log_db = Factory::getDB('zy_mc_log');
 			$v = array();
 			if ($arrival_no == $temp[0]['sell_no']) {
 				$v['arrival_state'] = 1;
 			}else{
 				$v['arrival_state'] = 2;
 			}
 			$zy_mc_log_db->update_zy_mc_log($v,$where);
 		}
 		return $re;
	}
}

?>